﻿using BLL;
using DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BONGDA
{
    public partial class frmChooseTours : Form
    {
        public int maSan = 0;
        public string tenSan = "";
        public string diaDiem = "";
        public string sucChua = "";
        public string namXay = "";
        public Image tours = null;

        public bool isChon = false;

        public frmChooseTours()
        {
            InitializeComponent();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            if (maSan != 0)
            {
                isChon = true;
            }
            else
            {
                isChon = false;
            }
            this.Close();
        }

        private void frmChooseTours_Load(object sender, EventArgs e)
        {
            HienThiDS();
            DataTable dt = SanVanDong_BLL.SelectAll();
            if (dt.Rows.Count == 0)
            {
                txtMa.Text = "";
                txtTen.Text = "";
            }
            else
            {
                try
                {
                    txtMa.Text = dt.Rows[0]["MaSan"].ToString();
                    txtTen.Text = dt.Rows[0]["TenSan"].ToString();

                    maSan = Convert.ToInt32(dt.Rows[0]["MaSan"].ToString());
                    tenSan = dt.Rows[0]["TenSan"].ToString();
                    diaDiem = dt.Rows[0]["DiaDiem"].ToString();
                    sucChua = dt.Rows[0]["SucChua"].ToString();
                    namXay = dt.Rows[0]["NamXay"].ToString();

                    Image FetchedImg;
                    MemoryStream stream = new MemoryStream((byte[])dt.Rows[0]["Tour"]);
                    FetchedImg = Image.FromStream(stream);
                    tours = FetchedImg;
                }
                catch (Exception)
                {
                }

            }
        }
        private void HienThiDS()
        {
            DataTable dt = SanVanDong_BLL.SelectTen();
            dgvSanVanDong.DataSource = dt;
        }

        private void dgvSanVanDong_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvSanVanDong.SelectedRows.Count > 0)
                {
                    SanVanDong_DTO dto = SanVanDong_BLL.SelectByID(
                                Convert.ToInt32(dgvSanVanDong.SelectedRows[0].Cells[0].Value));
                    txtMa.Text = dto.MaSan.ToString();
                    txtTen.Text = dto.TenSan;

                    maSan = dto.MaSan;
                    tenSan = dto.TenSan;
                    diaDiem = dto.DiaDiem;
                    sucChua = dto.SucChua.ToString();
                    namXay = dto.NamXay.ToString();

                    Image FetchedImg;
                    MemoryStream stream = new MemoryStream(dto.ByteTours);
                    FetchedImg = Image.FromStream(stream);
                    tours = FetchedImg;
                }
            }
            catch (Exception)
            {
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            isChon = false;
            this.Close();
        }

    }
}
